﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using System.IO;

namespace ANLP.ANTLR.ParserGenerator
{
    public partial class MainPage : UserControl
    {
        public MainPage()
        {
            InitializeComponent();
            StringBuilder sb = new StringBuilder();
            ANLP.Parser.ParserGenerator.Generate(new StringReader(@"
%Namespace ANLP.ANTLR
%Visibility public
%ExpressionType Expression
%token Colon,Star,QuestionMark,Quotes,Returns,Pipe,SemiColon,Identifier,Plus
%using ANLP.ANTLR.Expressions
%using ANLP

Program: { this.program=new Grammar(); } Statements; 

Statements : Statements Statement { AddStatement(expressions,1); }
          | Statement { AddStatement(expressions,0); }
          ; 

Statement : Identifier { $0 = new RuleStatement(lexer.GetText()); } Returns Identifier Colon StatementValues SemiColon { AddStatements(expressions); };

StatementValues : { $$=new StateExpression(); } StatementValues StatementValue { ((StateExpression)$$).Add($1); } StatementValuesOpt { ((StateExpression)$$).Add($2); }
    | StatementValue { ((StateExpression)$$).Add($0); } StatementValuesOpt { ((StateExpression)$$).Add($1); }
    ;

StatementValuesOpt : Pipe StatementValues
    |
    ;

StatementValue : Tokens { $$=$0; };

Tokens : { $$=new RuleStatement(""""); } Tokens Token { ((RuleStatement)$$).Add((Token)$1); } CardinalityOpt { ((Token)$1).Cardinality = (CardinalityExpression)$2; }
       | Token { ((RuleStatement)$$).Add((Token)$0); } CardinalityOpt { ((Token)$0).Cardinality = (CardinalityExpression)$1; }
       ;

CardinalityOpt : Cardinality { $$=$0; }
               |             { $$=null; }
               ;

Cardinality : Star { $$ = CardinalityExpression.ZeroOrMany; }
            | Plus { $$ = CardinalityExpression.OneOrMany; }
            | QuestionMark { $$ = CardinalityExpression.ZeroOrOne; }
            ;
               
    

Token : Identifier { $$ = new RuleReference(lexer.GetText()); }
      | Quotes Identifier { $$ = new LiteralToken(lexer.GetText()); } Quotes
      ;
"), new StringWriter(sb));
            output.Text = sb.ToString();

        }
    }
}
